//
//  CloudReserveDateViewModel.swift
//  EyeVisionForPatients
//
//  Created by PMC on 2020/2/27.
//  Copyright © 2020 tao Tao. All rights reserved.
//

import UIKit
import HandyJSON

struct CloudReserveDateModel: HandyJSON {
    var schDate: String?
    var docName: String?
    var doctorId: String?
    var deptId: String?
    var times: [CloudReserveDateList]?
}

struct CloudReserveDateList: HandyJSON {
    var dockingType: Int = -1
    /// 0-不可点 1-可点
    var flag: Int = 0
    var price: String?
    /// 排班时间
    var schTime: String?
    var scheduleId: String?
}

struct CloudReserveDateViewModel {
    let model: CloudReserveDateModel
    
    var dateList: [CloudReserveDateList] { return model.times ?? [] }
    
    var numberOfItems: Int { return dateList.count }
    
    var itemSize: CGSize {
        let itemW = (screenW - 32 * 2 - 20) / 2
        return CGSize(width: itemW, height: 36)
    }
    
    func viewModel(for indexPath: IndexPath) -> CloudReserveDateListViewModel {
        return CloudReserveDateListViewModel(model: dateList[indexPath.item])
    }
    
    func viewModelForRegistration(indexPath: IndexPath) -> CloudReserveDateListViewModel {
        return CloudReserveDateListViewModel(model: dateList[indexPath.row])
    }
    
    static func getReserveDateList(doctorId: String,
                                   departmentId: String,
                                   scheduleDate: String,
                                   timeInterval: String,
                                   businessType: String = "0",
                                   dockingType: String = "1",
                                   success: @escaping (_ model: CloudReserveDateModel) -> Void,
                                   failure: @escaping (_ errorMsg: String?) -> Void) {
        let paras = [
            "docId": doctorId,
            "deptId": departmentId,
            "schDate": scheduleDate,
            "timeInterval": timeInterval,
            "businessType": businessType,
            "dockingType": dockingType
        ]
        YSGLog("预约挂号号源: \(paras)")
        HttpTool.shared.request(target: ApiManager.reserveDateList(paras: paras), success: { (json, code) in
            YSGLog("预约挂号号源: \(json)")
            switch code {
            case .failure:
                failure(json["message"].description)
            case .success:
                if let model = JSONDeserializer<CloudReserveDateModel>.deserializeFrom(json: json["data"].description) {
                    success(model)
                } else {
                    failure("请求数据发生错误，稍后重试")
                }
            }
        }) { errorMsg in
            failure(errorMsg)
        }
    }
}

struct CloudReserveDateListViewModel {
    let model: CloudReserveDateList
    
    var flag: Int { return model.flag }
    var scheduleTime: String { return model.schTime ?? "" }
    var scheduleId: String { return model.scheduleId ?? "" }
    
    var isEnabled: Bool { return flag == 1 ? true : false }
    var textColor: UIColor { return flag == 1 ? .mainColor : .textGray }
    
    func scheduleTimeText() -> NSAttributedString {
        let text = scheduleTime + " 时间仅供参考，以医院实际情况为准"
        let attStr = NSMutableAttributedString(string: text)
        let attributes = [NSAttributedString.Key.foregroundColor: UIColor(hex: "#F8AF0F"), NSAttributedString.Key.font: UIFont.regular(9)]
        attStr.setAttributes(attributes, range: NSRange(location: text.count - 16, length: 16))
        return attStr
    }
    
}
